//
//  SortView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/24.
//  Copyright © 2017年 maopu. All rights reserved.
// 自定义排序view

import UIKit



protocol SortViewDelegate:class {
    func sortView(sortView:SortView , didSelectRowAt indexPath:Int , title:String)
}

class SortView: UIView {
    fileprivate var backgroundView : UIView!
    fileprivate var contentView : UIView!
    fileprivate var titleStr:[String] = []
    fileprivate var hintStrList:[String] = []
    
    weak var delegate:SortViewDelegate?

    init(type:Int){
        super.init(frame: UIScreen.main.bounds)
        self.initView(type)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initView(_ type:Int){
        if type == 0{
            titleStr = ["按正序排序"  , "按倒序排序"]
        }else{
            titleStr = ["按正序排序"  , "按倒序排序" , "按最热排序"]
        }
        hintStrList = ["正序"  , "倒序" , "最热"]
        
        self.backgroundView = UIView.init(frame: self.frame)
        self.backgroundView.backgroundColor = UIColor.black
        self.addSubview(self.backgroundView)
        
        let cancelRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(self.handleTap(_:)))
        cancelRecognizer.numberOfTapsRequired = 1
        self.backgroundView.addGestureRecognizer(cancelRecognizer)
        
        self.contentView = UIView.init(frame: CGRect(x: 0, y: AppHeight - (50 * CGFloat(self.titleStr.count)), width: AppWidth, height: 50 * CGFloat(self.titleStr.count)))
        self.contentView.alpha = 0
        self.contentView.backgroundColor = UIColor.white
        self.addSubview(self.contentView)
        
        for i in 0..<self.titleStr.count{
            let button = UIButton.init(type: .custom)
            button.tag = i
            button.frame = CGRect.init(x: 0, y: 50.0 * CGFloat(i), width: AppWidth, height: 50)
            button.setTitle(self.titleStr[i], for: UIControlState())
            button.setTitleColor(UIColor.m_102Color(), for: UIControlState())
            button.titleLabel?.font = UIFont.LightDefaultFont(15)
            button.addTarget(self, action: #selector(self.didRowBtnClick(button:)), for: .touchUpInside)
            self.contentView.addSubview(button)
            
            let lineView = UIView.init(frame: CGRect.init(x: 0, y: button.frame.maxY, width: AppWidth, height: 1))
            lineView.backgroundColor = UIColor.m_lineColor()
            self.contentView.addSubview(lineView)
        }
    }
    
    @objc fileprivate func didRowBtnClick(button:UIButton){
        if self.delegate != nil{
            self.delegate!.sortView(sortView: self, didSelectRowAt: button.tag , title: self.hintStrList[button.tag])
        }
        self.close()
    }
    
    @objc fileprivate func handleTap(_ gesture:UITapGestureRecognizer) -> Void{
        self.close()
    }
    
    func show(){
        UIApplication.shared.keyWindow?.addSubview(self)
        self.showBackground()
        self.showContentView()
    }
    
    func close() -> Void{
        self.hideContentView()
        self.hideBackground()
    }
    
    fileprivate func showBackground() -> Void{
        self.backgroundView.alpha = 0.0
        UIView.animate(withDuration: 0.5, animations: {
            self.backgroundView.alpha = 0.6
        })
    }
    
    fileprivate func showContentView(){
        UIView.animate(withDuration: 0.5, animations: {
            self.contentView.alpha = 1
        })
    }
    
    fileprivate func hideBackground() -> Void{
        
        UIView.animate(withDuration: 0.5, animations:{
            self.backgroundView.alpha = 0.0
        }, completion: { (animate) in
            self.removeFromSuperview()
        })
    }
    
    fileprivate  func hideContentView() -> Void{
        UIView.animate(withDuration: 0.5, animations: {
            self.contentView.alpha = 0
        })
    }
}
